package com.myq.server.exception;

import com.myq.server.pojo.RespBean;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

/**
 * @program:yeb
 * @author: 马雨清
 * @Time: 2023/4/27  17:47
 * @description: 全局异常处理
 */

@RestControllerAdvice
public class GlobalException {


    @ExceptionHandler(SQLException.class)
    public RespBean myqSqlException(SQLException e){
        if (e instanceof MySQLIntegrityConstraintViolationException){
            return RespBean.error(4001,"该数据有关联数据,操作失败！");
        }
        return RespBean.error("数据库异常,操作失败");
    }
}
